package com.party.mobile.web.controller.member;


import com.google.common.collect.Maps;
import com.party.authorization.annotation.Authorization;
import com.party.common.utils.StringUtils;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.biz.member.CMemberBuyBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: party
 * @description: 社群会员购买控制器
 * @author: Hua Qunhao
 * @create: 2020-04-10 12:11
 **/
@Controller
@RequestMapping(value = "member/buy")
public class CMemberBuyController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CMemberBuyBizService memberBuyBizService;
    @Autowired
    private CurrentUserBizService currentUserBizService;

    /**
     * 获取商品
     *
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "/getCommodity")
    public AjaxResult getCommodity(String commodityCode) {
        Map<String, String> commodity = Maps.newHashMap();
        if (StringUtils.isEmpty(commodityCode)) {
            commodityCode = "year";
        }
        try {
            List<Map<String, String>> commoditys = memberBuyBizService.getCommoditys(commodityCode);
            commodity = commoditys.get(0);
        } catch (Exception e) {
            logger.error("获取会员价格异常", e.getMessage());
            return AjaxResult.error(500, "获取会员价格异常");
        }
        return AjaxResult.success(commodity);
    }


    /**
     * 确认购买
     *
     * @return 交互数据
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "/confirmBuy")
    public AjaxResult confirmBuy(String commodityCode, HttpServletRequest request) {
        if (StringUtils.isEmpty(commodityCode)) {
            return AjaxResult.error(100, "code为空");
        }
        Map<String,String> commodityOrder = Maps.newHashMap();
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            List<Map<String, String>> prices = memberBuyBizService.getCommoditys(commodityCode);
            if (prices == null || prices.size() == 0) {
                return AjaxResult.error(101, "商品不存在");
            }
            Map<String, String> commodity = prices.get(0);
            if (Float.parseFloat(commodity.get("price")) < new Float(0.01f)) {
                return AjaxResult.error(102, "金额最低为0.01");
            }
            String orderId = memberBuyBizService.insertOrder(currentUser.getId(), commodity);
            commodityOrder.put("commodityCode",commodity.get("commodityCode"));
            commodityOrder.put("orderId",orderId);
        } catch (Exception e) {
            logger.error("确认购买异常", e.getMessage());
            return AjaxResult.error(500, "确认购买异常");
        }
        return AjaxResult.success(commodityOrder);
    }
}
